{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copyright 2019 Google LLC\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     https://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a target=\"_blank\" href=\"https://colab.research.google.com/github/GoogleCloudPlatform/keras-idiomatic-programmer/blob/master/workshops/Idiomatic%20Programmer%20-%20handbook%202%20-%20Codelab%202.ipynb\">\n",
    "<img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Idiomatic Programmer Code Labs\n",
    "\n",
    "## Code Labs #2 - Get Familiar with Data Augmentation\n",
    "\n",
    "## Prerequistes:\n",
    "\n",
    "    1. Familiar with Python\n",
    "    2. Completed Handbook 2/Part 8: Data Augmentation\n",
    "\n",
    "## Objectives:\n",
    "\n",
    "    1. Channel Conversion\n",
    "    2. Flip Images\n",
    "    3. Roll (Shift Images)\n",
    "    4. Rotate without Clipping"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup:\n",
    "\n",
    "Install the additional relevant packages to continuen with OpenCV, and then import them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install the matplotlib library for plotting\n",
    "!pip install matplotlib\n",
    "# special iPython command --tell's matplotlib to inline (in notebook) displaying plots\n",
    "%matplotlib inline\n",
    "\n",
    "# Adrian Rosenbrock's image manipulation library\n",
    "!pip install imutils\n",
    "\n",
    "# Import matplotlib python plot module\n",
    "import matplotlib.pyplot as plt\n",
    "# Import OpenCV\n",
    "import cv2\n",
    "# Import numpy scientific module for arrays\n",
    "import numpy as np\n",
    "# Import imutils\n",
    "import imutils"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Channel Conversions\n",
    "\n",
    "OpenCV reads in the channels as BGR (Blue, Green, Read) instead of the more common convention of RGB (Red, Green Blue). Let's learn how to change the channel ordering to RGB.\n",
    "    \n",
    "You fill in the blanks (replace the ??), make sure it passes the Python interpreter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from urllib.request import urlopen\n",
    "\n",
    "# Let's read in that apple image again.\n",
    "url = \"https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/master/workshops/apple.jpg\"\n",
    "request = urlopen(url)\n",
    "img_array = np.asarray(bytearray(request.read()), dtype=np.uint8)\n",
    "# Let's read in the image as a color image\n",
    "image = cv2.imdecode(img_array, cv2.IMREAD_COLOR)\n",
    "plt.imshow(image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### What, it's a blue apple!\n",
    "\n",
    "Yup. It's the same data, but since matplotlib presumes RGB, then blue is the 3rd channel, but in BGR -- that's the red channel.\n",
    "\n",
    "Let's reorder the channels from BGR to RGB and then display again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's convert the channel order to RGB\n",
    "# HINT: RGB should be a big giveaway.\n",
    "image = cv2.cvtColor(image, cv2.COLOR_BGR2??)\n",
    "plt.imshow(image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Flip Images\n",
    "\n",
    "Let's use OpenCV to flip an image (apple) vertically and then horizontally.\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Flip the image horizontally (upside down)\n",
    "# HINT: flip should be a big giveaway\n",
    "flip = cv2.??(image, 0)\n",
    "plt.imshow(flip)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Flip the image vertically (mirrored)\n",
    "# HINT: If 0 was horizontal, what number would be your first guess to be vertical?\n",
    "flip = cv2.flip(image, ??)\n",
    "plt.imshow(flip)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Roll (Shift) Images\n",
    "\n",
    "Let's use numpy to shift an image -- say 80 pixels to the right."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's shift the image vertical 80 pixels to the right, where axis=1 means along the width\n",
    "# HINT: another name for shift is roll\n",
    "roll = np.??(image, 80, axis=1)\n",
    "plt.imshow(roll)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's shift the image now horizontally 80 pixels down.\n",
    "# HINT: if shifting the width axis is a 1, what do you think the value is for \n",
    "# shifting along the height axis?\n",
    "roll = np.roll(image, 80, axis=??)\n",
    "plt.imshow(roll)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Randomly Rotate the Image (w/o Clipping)\n",
    "\n",
    "Let's use imutils to randomly rotate the image without clipping it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "# Let's get a random value between 0 and 60 degrees.\n",
    "degree = random.randint(0, 60)\n",
    "\n",
    "# Let's rotate the image now by the randomly selected degree\n",
    "rot = imutils.rotate_bound(image, ??)\n",
    "plt.imshow(rot)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## End of Code Lab"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
